package com.leyou.search.web;

import com.leyou.search.dto.SearchRequest;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.IndexService;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/goods")
public class SearchController {
    @Resource
    private  SearchService searchService;
    @Resource
    private IndexService indexService;


    /**
     * 初始化索引库
     */
    @GetMapping("/initialization")
    public ResponseEntity<String> init() {
        indexService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggest(@RequestParam("key") String key){
        return searchService.getSuggest(key);
    }

    @PostMapping("/list")
    public Mono<PageInfo<Goods>> listData(@RequestBody SearchRequest searchRequest){
        return searchService.listData(searchRequest);
    }

    @PostMapping("/filter")
    public Mono<Map<String,List<?>>> listFilter(@RequestBody SearchRequest searchRequest){
        return searchService.listFilter(searchRequest);
    }
}